package cn.linkstudy.util;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class TimeUtils {
    public static String formatSessionTime(Date date) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime sessionTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        Duration duration = Duration.between(sessionTime, now);

        long minutes = duration.toMinutes();
        long hours = duration.toHours();
        long days = duration.toDays();

        if (hours < 1) {
            return minutes + "分钟前";
        } else if (hours < 24) {
            return hours + "小时前";
        } else if (hours < 48) {
            return "昨天";
        } else if (hours < 72) {
            return "前天";
        } else if (days < 30) {
            return days + "天前";
        } else {
            long months = days / 30;
            return months + "月前";
        }
    }
}